/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * venderPropiedad.java
 *
 * Created on 20/05/2011, 01:03:26 AM
 */

package logica;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge Maldonado
 */
public class venderPropiedad extends javax.swing.JDialog {
    private ArrayList<usuario> usuarios;
    private DefaultTableModel mod;
    /** Creates new form venderPropiedad */
    public venderPropiedad(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public venderPropiedad(ArrayList<usuario> usuarios){
        this(null,true);
        this.usuarios=usuarios;
        for(int i=0;i<usuarios.size();i++){
            jComboBox1.addItem(usuarios.get(i).getNombre());
            jComboBox2.addItem(usuarios.get(i).getNombre());
        }
        llenarTabla(usuarios.get(0));
    }
    private void llenarTabla(usuario usu){
        mod=new DefaultTableModel(new Object[]{"Id","Nombre","Precio","Color"},0);
        for(int i=0;i<usu.getPropiedades().size();i++){
            if(usu.getPropiedades().get(i).getTipo()==propiedad.TIPO_LOTE&&((lote)usu.getPropiedades().get(i)).isHipotecada())
                continue;
            mod.addRow(new Object[]{usu.getPropiedades().get(i).getId(),usu.getPropiedades().get(i).getDescripcion(),usu.getPropiedades().get(i).getPrecio(),usu.getPropiedades().get(i).getTipo()==propiedad.TIPO_LOTE?((lote)usu.getPropiedades().get(i)).getColorTexto():"-"});
        }
        jTable1.setModel(mod);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Usuario");

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel2.setText("Vende a");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Vender");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 398, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        llenarTabla(usuarios.get(jComboBox1.getSelectedIndex()));
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        usuario comprador=usuarios.get(jComboBox2.getSelectedIndex());
        usuario vendedor=usuarios.get(jComboBox1.getSelectedIndex());
        if(comprador!=vendedor){
            if(jTable1.getSelectedRow()!=-1){
                 propiedad prop=null;
                 for(int i=0;i<vendedor.getPropiedades().size();i++){
                     if(jTable1.getValueAt(jTable1.getSelectedRow(), 0).equals(vendedor.getPropiedades().get(i).getId())){
                         prop=vendedor.getPropiedades().get(i);
                         break;
                     }
                 }
                 if(comprador.getDinero()>=prop.getPrecio()){                     
                     prop.Vender(comprador);
                     vendedor.setDinero(vendedor.getDinero()+prop.getPrecio());
                     comprador.setDinero(comprador.getDinero()-prop.getPrecio());
                     JOptionPane.showMessageDialog(null, "Venta efectuada con éxito", "Atención", JOptionPane.INFORMATION_MESSAGE);
                     llenarTabla(usuarios.get(jComboBox1.getSelectedIndex()));
                 }else{
                     JOptionPane.showMessageDialog(null, "Error no hay fondos suficientes para efectuar la venta", "Error", JOptionPane.ERROR_MESSAGE);
                 }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
